package com.daatak.mvc.application;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.daatak.application.business.ApplicationBeanI;
import com.daatak.application.domain.ApplicationObject;
import com.daatak.application.domain.ApplicationView;
import com.daatak.application.domain.AssetObject;
import com.daatak.mvc.common.JsonResponse;
import com.daatak.mvc.validator.ApplicationMinimumDataValidator;
import com.daatak.mvc.validator.ApplicationValidator;
import com.daatak.mvc.validator.AssetValidator;
import com.daatak.mvc.validator.LoanValidator;

@Controller
public class ApplicationController {
	
	
	@Autowired
	ApplicationBeanI applicationBean;
		
	@InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true));
    }
	
	
	@RequestMapping(value="/application/home", method = RequestMethod.GET)
	public String applicationHome(@ModelAttribute ApplicationPojo applicationPojo,BindingResult result, ModelMap model) {
		
		return "home.application/applicationGrid";
	}
	@RequestMapping(value="/application/details", method = RequestMethod.GET)
	public String userDetails(ModelMap model, HttpServletRequest request) {
		
		ApplicationObject application = applicationBean.findById(Integer.parseInt((String)request.getParameter("id")));
		
		model.addAttribute("application", application);
		
		return "ajax.application/applicationDetails";
	}
	@RequestMapping(value="/application/applicationGrid", method = RequestMethod.GET)
	public @ResponseBody List<ApplicationView> listApplication(ModelMap model) {
		return applicationBean.list();
		
	}
	
	@RequestMapping(value="/application/getInputScreen/{id}", method = RequestMethod.GET)
	public String input(@PathVariable("id") Integer applicationId,
						ModelMap model) {
		
		ApplicationPojo pojo=new ApplicationPojo();
		if(applicationId>0){
			applicationBean.loadApplication(pojo,applicationId);
		}
		
		model.addAttribute("applicationPojo",pojo);
		
		return "ajax.application/applicationForm";
		
	}
	@RequestMapping(value="/application/details/{id}", method = RequestMethod.GET)
	public String applicationDetails(@PathVariable("id") Integer applicationId,
						ModelMap model) {
		
		ApplicationPojo pojo=new ApplicationPojo();
		if(applicationId>0){
			applicationBean.loadApplication(pojo,applicationId);
		}
		
		model.addAttribute("applicationPojo",pojo);
		
		return "ajax.application/applicationDetails";
		
	}

	@RequestMapping(value="/application/getInputScreen", method = RequestMethod.GET)
	public String newForm(
						ModelMap model) {
		
		ApplicationPojo pojo=new ApplicationPojo();
		
		model.addAttribute("applicationPojo",pojo);
		
		return "ajax.application/applicationForm";
		
	}
	
	@RequestMapping(value="/application/add/subsection", method = RequestMethod.GET)
	public String loadedInput(@RequestParam(value="application.applicationId",required=false,defaultValue="0") Integer applicationId,
			ModelMap model) {
		
			ApplicationPojo pojo=new ApplicationPojo();
			if(applicationId>0){
			applicationBean.loadApplication(pojo,applicationId);
			}
		model.addAttribute("applicationPojo",pojo);
		
		return "ajax.application/applicationSection";
		
	}
	
	@RequestMapping(value="/application/add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse submit(@ModelAttribute ApplicationPojo applicationPojo,
						 BindingResult result, 
						 ModelMap model) {
	
		JsonResponse res = new JsonResponse();
		ApplicationMinimumDataValidator minimumValidator = new ApplicationMinimumDataValidator();
		
		
		minimumValidator.validate(applicationPojo, result);
		
		if(!result.hasErrors()){
			applicationPojo.getCustomer().setReferenceType("APPLICATION");
			applicationBean.create(applicationPojo);
			res.setObjectName("application.applicationId");
			res.setObjectId(String.valueOf(applicationPojo.getApplication().getApplicationId()));
			res.setStatus("SUCCESS");
		}
		
		ApplicationValidator validator = new ApplicationValidator();
		validator.validate(applicationPojo, result);
		
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			  res.setStatus("ERROR");
		}
		
		
		
		return res;
		
	}
	
	@RequestMapping(value="/application/asset/add", method = RequestMethod.GET)
	public String assetSelection(@ModelAttribute ApplicationPojo applicationPojo, ModelMap model) {
		
		//ApplicationPojo pojo = new ApplicationPojo();
		model.addAttribute("application",applicationPojo);
		
		return "home.application/application_asstes";
		//return URLResultResolver.resolvePath(actionType, URLPathConstants.APPLICATION,URLPathConstants.FORM);
	}
	
	@RequestMapping(value="/application/productSelection", method = RequestMethod.POST)
	public String productSelection(@ModelAttribute ApplicationPojo applicationPojo, ModelMap model) {
		
		//ApplicationPojo pojo = new ApplicationPojo();
		applicationPojo.setFinance(applicationBean.loadApplicationFinancial(applicationPojo.getApplication().getApplicationId()));
		model.addAttribute("application",applicationPojo);
		//model.addAttribute("financeOption",applicationBean.financingOptionDetail());
		model.addAttribute("financeSummary",applicationBean.loadApplicationFinancial(applicationPojo.getApplication().getApplicationId()));
		
		//System.out.println("Inside productselection method of application controller---------------"+applicationPojo.getApplication().getApplicationId());
		model.addAttribute("assets", applicationBean.listAssetByAppId(71));
		
		return "ajax.application/applicationProductSelection";
		//return URLResultResolver.resolvePath(actionType, URLPathConstants.APPLICATION,URLPathConstants.FORM);
	}
	

	@RequestMapping(value="/application/applicationAssets", method = RequestMethod.GET)
	public @ResponseBody List<AssetObject> applicationAssets(@RequestParam(value="id",required=false,defaultValue="0") Integer applicationId,
	ModelMap model) {
		
		return applicationBean.listAssetByAppId(applicationId);
		
		
	}
	
	@RequestMapping(value="/application/applicationSubmission", method = RequestMethod.POST)
	public String applicationSubmission(@ModelAttribute ApplicationPojo applicationPojo, ModelMap model) {
		
		//ApplicationPojo pojo = new ApplicationPojo();
		applicationPojo.setFinance(applicationBean.loadApplicationFinancial(applicationPojo.getApplication().getApplicationId()));
		model.addAttribute("application",applicationPojo);
		//model.addAttribute("financeOption",applicationBean.financingOptionDetail());
		model.addAttribute("financeSummary",applicationBean.loadApplicationFinancial(applicationPojo.getApplication().getApplicationId()));
		
		
		return "ajax.application/applicationSubmission";
		//return URLResultResolver.resolvePath(actionType, URLPathConstants.APPLICATION,URLPathConstants.FORM);
	}
	@RequestMapping(value="/application/asset_add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse addAsset(@ModelAttribute ApplicationPojo applicationPojo,BindingResult result, ModelMap model) {
		
		JsonResponse res = new JsonResponse();
		AssetValidator validator = new AssetValidator();
		validator.validate(applicationPojo, result);
		
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			  res.setStatus("ERROR");
		}
		else{
			applicationBean.addAsset(applicationPojo);
			res.setStatus("ERROR");
		}
		
		return res;
	}
	@RequestMapping(value="/application/calculateAndSave", method = RequestMethod.POST)
	public @ResponseBody JsonResponse calculateAndSave(@ModelAttribute ApplicationPojo applicationPojo,BindingResult result, ModelMap model) {
		
		JsonResponse res = new JsonResponse();
		LoanValidator validator = new LoanValidator();
		validator.validate(applicationPojo, result);
		
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			  res.setStatus("ERROR");
		}
		else{
			applicationPojo.getFinance().setApplicationId(applicationPojo.getApplication().getApplicationId());
			applicationBean.calculateAndSave(applicationPojo);
			res.setObject(applicationPojo.getFinance());
			res.setStatus("SUCCESS");
		}
		
		return res;
	}
	
	/*@RequestMapping(value="financeOptionDetails", method = RequestMethod.GET)
	public @ResponseBody List<FinancialOptionView> financeOptionDetails(@ModelAttribute ApplicationPojo applicationPojo,BindingResult result, ModelMap model) {
		
		return applicationBean.financingOptionDetail();
	}*/
	@RequestMapping(value="/application", method = RequestMethod.GET)
	public String application(@ModelAttribute ApplicationPojo applicationPojo,BindingResult result, ModelMap model) {
		
		return "ajax.application/applicationGrid";
	}

	public void setApplicationBean(ApplicationBeanI applicationBean) {
		this.applicationBean = applicationBean;
	}
	
}
